//
//  EvaluateListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/21.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "StoreDetailEvaluationCell.h"
#import "StoreDetailEvaluationHeader.h"
#import "StoreDetailEvaluationHeader.h"
#import "EvaluationDetailViewController.h"

#import "EvaluateListViewController.h"

@interface EvaluateListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *allTab;

@property (weak, nonatomic) IBOutlet UIButton *goodTab;

@property (weak, nonatomic) IBOutlet UIButton *middleTab;

@property (weak, nonatomic) IBOutlet UIButton *negativeTab;

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <StoreDetailEvaluateModel *>*dataArray;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation EvaluateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self refreshParam];
    [self request_EvaList];
}

- (void)refreshParam {
    self.pageNum = 1;
    self.pageSize = 20;
}

- (void)initView {
    
    self.title = @"评价列表";
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor =  UIColorHex(0xf1f1f1);
    
    self.tableview.showsHorizontalScrollIndicator = NO;
    self.tableview.showsVerticalScrollIndicator = NO;
    
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.estimatedRowHeight = 0;
    
    [_tableview registerNib:[UINib nibWithNibName:@"StoreDetailEvaluationCell" bundle:nil] forCellReuseIdentifier:@"StoreDetailEvaluationCellID"];

    WEAKSELF
    _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf refreshParam];
        [weakSelf request_EvaList];
    }];

    _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.pageNum ++;
        [weakSelf request_EvaList];
    }];
    _tableview.mj_footer.ignoredScrollViewContentInsetBottom = Is_IPhoneX ? Bottom_SafeHeight : CGFLOAT_MIN;
}


#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return self.dataArray[section].itemHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    StoreDetailEvaluationHeader *headView = [[NSBundle mainBundle] loadNibNamed:@"StoreDetailEvaluationHeader" owner:nil options:nil].firstObject;
    [headView fillHeadWithModel:self.dataArray[section]];
    WEAKSELF
    [headView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
       
        if (weakSelf.dataArray.count > section) {
            EvaluationDetailViewController *vc = [[EvaluationDetailViewController alloc] init];
            vc.ueId = weakSelf.dataArray[section].ueId;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    }];
    
    return  headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreDetailEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreDetailEvaluationCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSMutableArray<StoreDetailEvaluateModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_EvaList {
            
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];

    NSMutableDictionary *t = @{}.mutableCopy;

    [t setValue:self.goodsId forKey:@"ueGoodsId"];
    [t setValue:self.storeId forKey:@"ueSellerId"];
    [t setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    [dic setValue:t forKey:@"t"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_EvaluateList];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [weakSelf.tableview.mj_header endRefreshing];
         [weakSelf.tableview.mj_footer endRefreshing];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];

                if ([list isKindOfClass:[NSArray class]]) {
                    
                    if (weakSelf.pageNum == 1) {
                        weakSelf.dataArray = @[].mutableCopy;
                    }
                    
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        StoreDetailEvaluateModel *model = [StoreDetailEvaluateModel modelWithDictionary:listDic];
                        
                        NSArray *imageUrls = [listDic valueForKey:@"imageUrls"];

                        if ([imageUrls isKindOfClass:[NSArray class]]) {
                            model.imageUrls = @[].mutableCopy;
                            for (int j = 0; j < imageUrls.count; j++) {
                                NSDictionary *imgDic = imageUrls[j];
                                CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imgDic];
                                [model.imageUrls addObject:imgModel];
                            }
                        }
                        
                        model.infoHeight = 60.0f;
                        model.evaluateTextHeight = [Utilities getStringHeightWithText:model.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30] + 20;
                        NSInteger lineCount = model.imageUrls.count/3;
                        if (model.imageUrls.count%3>0) {
                            lineCount++;
                        }
                        model.collectionViewHeight = lineCount * ((TTScreenWidth - 30)/3);
                        model.thumbHeight = CGFLOAT_MIN;
                        model.packageHeight = CGFLOAT_MIN;
                        
                        model.itemHeight = model.infoHeight + model.evaluateTextHeight + model.collectionViewHeight + model.packageHeight + model.thumbHeight;
                        
                        [weakSelf.dataArray addObject:model];
                    }
                    
                    [weakSelf.tableview reloadData];
                }
                
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [weakSelf.tableview.mj_header endRefreshing];
         [weakSelf.tableview.mj_footer endRefreshing];
    }];
}



@end
